package com.scs.controller;

import com.alibaba.fastjson.JSONObject;
import com.scs.pojo.Identity;
import com.scs.service.IdentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/Identity")
public class IdentityController {
    @Autowired
    private IdentityService identityService;

    /**
     * 添加职位信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addIdentity", produces = "application/json;charset=utf-8")
    public String addIdentity(HttpServletRequest request) {
        JSONObject data = new JSONObject();
        String identity = request.getParameter("identity");
        String description = request.getParameter("description");
        if (identity == null || description == null || identity.equals("") || description.equals("")) {
            data.put("msg", "添加信息为空");
            data.put("data", "");
            data.put("success", 0);
            return data.toJSONString();
        }
        Identity oneIdentity = new Identity(null, identity, description);
        int addNumber = identityService.addIdentity(oneIdentity);
        if (addNumber != 0) {
            data.put("msg", "添加成功");
            data.put("data", "");
            data.put("success", 1);
            return data.toJSONString();
        }
        data.put("msg", "添加失败");
        data.put("data", "");
        data.put("success", 0);
        return data.toJSONString();
    }

    /**
     * 获取所有职位信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllIdentity", produces = "application/json;charset=utf-8")
    public String getAllIdentity(HttpServletRequest request) {
        JSONObject data = new JSONObject();
        List<Identity> allIdentity = identityService.getAllIdentity();
        if (allIdentity.size() == 0) {
            data.put("msg", "没有职位信息");
            data.put("data", "");
            data.put("code", 110);
            data.put("count", 0);
            return data.toJSONString();
        }
        data.put("msg", "获取职位信息成功");
        data.put("data", allIdentity);
        data.put("code", 0);
        data.put("count", allIdentity.size());
        return data.toJSONString();
    }

    /**
     * 删除职位信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteIdentity", produces = "application/json;charset=utf-8")
    public String deleteIdentity(HttpServletRequest request) {
        JSONObject data = new JSONObject();
        Integer identityId = Integer.parseInt(request.getParameter("identityId"));
        if (identityId == null) {
            data.put("msg", "获取identityId失败");
            data.put("data", "");
            data.put("success", 0);
            return data.toJSONString();
        }
        int deleteNumber = identityService.deleteIdentity(identityId);
        if (deleteNumber == 0) {
            data.put("msg", "删除失败");
            data.put("data", "");
            data.put("success", 0);

        } else {
            data.put("msg", "删除成功");
            data.put("data", "");
            data.put("success", 1);
        }
        return data.toJSONString();
    }

    /**
     * 更新职位信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateIdentity", produces = "application/json;charset=utf-8")
    public String updateIdentity(HttpServletRequest request) {
        JSONObject data = new JSONObject();
        Integer identityId = Integer.parseInt(request.getParameter("identityId"));
        String identity = request.getParameter("identity");
        String description = request.getParameter("description");
        if (identityId == null) {
            data.put("msg", "获取identityId失败");
            data.put("data", "");
            data.put("success", 0);
            return data.toJSONString();
        }
        int updateNumber = identityService.updateIdentity(identity, description, identityId);
        if (updateNumber == 0) {
            data.put("msg", "更新失败");
            data.put("data", "");
            data.put("success", 0);
            return data.toJSONString();
        }
        data.put("msg", "更新成功");
        data.put("data", "");
        data.put("success", 1);
        return data.toJSONString();
    }
}
